//
//  RegisterWithEmailViewController.m
//  Daybook
//
//  Created by dllo on 16/6/1.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "RegisterWithEmailViewController.h"
#import "Common.h"
#import <BmobSDK/Bmob.h>
#import "RegisterViewController.h"
@interface RegisterWithEmailViewController ()

@property (nonatomic, strong)UITextField *userEmailTextField;

@property (nonatomic, strong)UITextField *nicknameTextField;

@property (nonatomic, strong)UITextField *passwordTextField;



@property (nonatomic, strong)UIButton *getCaptchaButton;



@end

@implementation RegisterWithEmailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createView];
}

- (void)createView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(10, 34, 25, 25);
    [self.view addSubview:backButton];
    backButton.backgroundColor = [UIColor clearColor];
    
    [backButton setImage:[UIImage imageNamed:@"backArrow"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(clickBack:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
//    UIButton *registerButton  = [UIButton buttonWithType:UIButtonTypeCustom];
//    registerButton.frame = CGRectMake(MAIN_WIDTH - 50, 34, 40, 25);
//    registerButton.backgroundColor = [UIColor whiteColor];
//    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
//    [registerButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
//    [registerButton addTarget:self action:@selector(clickRegister:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:registerButton];
    
    
    self.userEmailTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 100, MAIN_WIDTH - 20, 40)];
    self.userEmailTextField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.userEmailTextField];
    
    self.userEmailTextField.placeholder = @"用户名/邮箱";
    
    
//    self.getCaptchaButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.getCaptchaButton.frame = CGRectMake(MAIN_WIDTH - 80, 105, 60, 30);
//    self.getCaptchaButton.backgroundColor = [UIColor lightGrayColor];
//    [self.getCaptchaButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [self.getCaptchaButton setTitle:@"获取验证码" forState:UIControlStateNormal];
//    //getCaptchaButton.font = [UIFont systemFontOfSize:11];
//    self.getCaptchaButton.titleLabel.font = [UIFont systemFontOfSize:11];
//    
//    self.getCaptchaButton.layer.cornerRadius = 5;
//    self.getCaptchaButton.layer.masksToBounds = YES;
//    [self.getCaptchaButton addTarget:self action:@selector(getCaptch:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:self.getCaptchaButton];
    
    
    
    
    self.nicknameTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 140, MAIN_WIDTH - 20, 40)];
    self.nicknameTextField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.nicknameTextField];
    self.nicknameTextField.placeholder = @"请输入昵称";
    
    
    
    
    
        self.passwordTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 180, MAIN_WIDTH - 20, 40)];
        self.passwordTextField.backgroundColor = [UIColor whiteColor];
        self.passwordTextField.placeholder = @"请输入密码";
        self.passwordTextField.secureTextEntry = YES;
        [self.view addSubview:self.passwordTextField];
    
    
    
    
    
    UIView *firstLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 100, MAIN_WIDTH, 1)];
    firstLineView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:firstLineView];
    
    UIView *secLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 140, MAIN_WIDTH, 1)];
    secLineView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:secLineView];
    
    
    UIView *thirdLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 180, MAIN_WIDTH, 1)];
    thirdLineView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:thirdLineView];
    
    UIView *fourLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 220, MAIN_WIDTH, 1)];
    fourLineView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:fourLineView];
    
    
    UIButton *finishedRegisterButton  = [UIButton buttonWithType:UIButtonTypeCustom];
    finishedRegisterButton.frame = CGRectMake(10, 240, MAIN_WIDTH - 20, 40);
    finishedRegisterButton.backgroundColor  = [UIColor orangeColor];
    [self.view addSubview:finishedRegisterButton];
    
    [finishedRegisterButton setTitle:@"完成注册" forState:UIControlStateNormal];
    [finishedRegisterButton addTarget:self action:@selector(clickFinishRegister:) forControlEvents:UIControlEventTouchUpInside];
    finishedRegisterButton.layer.cornerRadius = 5;
    finishedRegisterButton.layer.masksToBounds = YES;
    
    
    UIButton *emailRegisterButton = [UIButton buttonWithType:UIButtonTypeCustom];
    emailRegisterButton.frame = CGRectMake(MAIN_WIDTH / 2 - 50, MAIN_HEIGHT - 60, 100, 40);
    emailRegisterButton.backgroundColor = [UIColor clearColor];
    [self.view addSubview:emailRegisterButton];
    
    [emailRegisterButton setTitle:@"手机号登录" forState:UIControlStateNormal];
    [emailRegisterButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [emailRegisterButton addTarget:self action:@selector(clickEmailRegister:) forControlEvents:UIControlEventTouchUpInside];
    

}


- (void)clickBack:(UIButton *)button {
    [self dismissViewControllerAnimated:YES completion:^{
        
        
        
    }];
}


- (void)clickFinishRegister:(UIButton *)button {
    
    NSString *userName = self.userEmailTextField.text;
    NSString *userPassword = self.passwordTextField.text;
    
    if ([userPassword isEqualToString:@""]) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"密码不能为空" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        
        [alertController addAction:alertAction];
        [self presentViewController:alertController animated:YES completion:^{
            
            
        }];

    }
    
    
    BmobUser *bUser = [[BmobUser alloc] init];
    [bUser setUsername:userName];
    [bUser setPassword:userPassword];
    [bUser setEmail:userName];

    [bUser signUpInBackgroundWithBlock:^ (BOOL isSuccessful, NSError *error){
        if (isSuccessful){
            NSLog(@"Sign up successfully");
            

            


            [[NSNotificationCenter defaultCenter] postNotificationName:@"userLogin" object:nil userInfo:@{@"nickname": self.nicknameTextField.text}];
            

            self.presentingViewController.view.alpha = 0;
            [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"headChanged" object:nil];

            
            
            
        } else {
            NSLog(@"%@",error);
        }
    }];
    
    
    
    BmobUser *user = [BmobUser getCurrentUser];
    //应用开启了邮箱验证功能
   //if ([user objectForKey:@"emailVerified"]) {
        //用户没验证过邮箱
        if (![[user objectForKey:@"emailVerified"] boolValue]) {
            [user verifyEmailInBackgroundWithEmailAddress:[NSString stringWithFormat:@"www.%@", userName]];
            
            
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请查看邮箱验证您的邮箱地址" message:nil preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                
            }];
            
            [alertController addAction:alertAction];
            [self presentViewController:alertController animated:YES completion:^{
                
                
            }];

            
            
            
        }
    
    
    
        

        
    //}
    
}

- (void)clickEmailRegister:(UIButton *)button {
    RegisterViewController *registerVC = [[RegisterViewController alloc]init];
    [self presentViewController:registerVC animated:YES completion:^{
        
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
